﻿package com.truongnha.clienttn.presentation;

import com.markupartist.android.widget.ActionBar;
import com.truongnha.clienttn.R;
import com.truongnha.clienttn.business.model.UserModel;
import com.truongnha.clienttn.business.service.LogoutService;
import com.truongnha.clienttn.business.service.MainScreenService;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

public class MainScreenActivity extends TabActivity {
	EditText etMessage1;
	EditText etMessage2;
	Integer[] arrayPointSize = { 10, 20, 30, 40, 50 };
	private TabHost tabHost;
	private UserModel user;
	private IntentFilter intentFilter;
	private BroadcastReceiver intentReceiver;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_screen);
		MainScreenService mainScreenService = new MainScreenService(
				getApplicationContext());
		user = mainScreenService.getUser();

		// 2.Create action bar for this activity:
		final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
		actionBar.setTitle("Trường Nhà");
		actionBar.setProgressBarVisibility(View.GONE);
		// 3.Custom intentReceiver
		
		intentReceiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				if (intent.getExtras().getBoolean("doneLoading"))
					actionBar.setProgressBarVisibility(View.GONE);
				else
					actionBar.setProgressBarVisibility(View.VISIBLE);
			}
		};
		intentFilter = new IntentFilter();
		intentFilter.addAction("DONE_LOADING_ACTION");
		// ---register the receiver---
		registerReceiver(intentReceiver, intentFilter);
		
		
		if (user.getPosition().equals("STUDENT")) {
			Intent intent;
			tabHost = getTabHost(); // The activity TabHost
			tabHost.setBackgroundResource(R.drawable.list_selector);
			TabHost.TabSpec spec;
			
			intent = new Intent().setClass(this, StudentMarkFrag.class);
			spec = tabHost.newTabSpec("Điểm").setContent(intent);
			spec.setIndicator(createTabView(this, "Điểm",
					R.drawable.timetable_icon));
			tabHost.addTab(spec);

			intent = new Intent().setClass(this, StudentMarkByTimeFrag.class);
			spec = tabHost.newTabSpec("Điểm theo ngày").setContent(intent);
			spec.setIndicator(createTabView(this, "Điểm theo ngày",
					R.drawable.timetable_icon));
			tabHost.addTab(spec);
			
			// tab3: attend
			intent = new Intent().setClass(this, StudentAttendanceFrag.class);
			spec = tabHost.newTabSpec("Điểm danh").setContent(intent);
			spec.setIndicator(createTabView(this, "Điểm danh",
					R.drawable.timetable_icon));
			tabHost.addTab(spec);
			
		} else {
			
			Log.d("log"," Main screen actitity 1");
			Intent intent;
			tabHost = getTabHost(); // The activity TabHost
			tabHost.setBackgroundResource(R.drawable.list_selector);
			TabHost.TabSpec spec;
			
			Log.d("log"," Main screen actitity 2");
			
			intent = new Intent().setClass(this, TeacherAttendaceFrag.class);
			spec = tabHost.newTabSpec("Điểm danh").setContent(intent);
			spec.setIndicator(createTabView(this, "Điểm danh",
					R.drawable.timetable_icon));
			tabHost.addTab(spec);

			Log.d("log"," Main screen actitity 3");

			intent = new Intent().setClass(this, TeacherAttendaceFrag.class);
			spec = tabHost.newTabSpec("Nhập điểm").setContent(intent);
			spec.setIndicator(createTabView(this, "Nhập điểm",
					R.drawable.timetable_icon));
			tabHost.addTab(spec);
			
			Log.d("log"," Main screen actitity 4");
			
		}
	} // onCreate

	// set the option menu for the current activity
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		int groupId = 0;
		// only one Option menu per activity
		menu.add(groupId, 1, 1, "Thông tin cá nhân");
		menu.add(groupId, 2, 2, "Đăng xuất");
		return super.onCreateOptionsMenu(menu);
	}

	// called whenever an item in your options menu is selected
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return (applyMenuOption(item) || super.onOptionsItemSelected(item));

	}

	// apply the action associated to selected item
	private boolean applyMenuOption(MenuItem item) {
		int menuItemId = item.getItemId(); // 1, 2, 3, ...11

		if (menuItemId == 1) {
			Log.d("LOG", "thong tin ca nhan 0");
			Intent i = new Intent(getApplicationContext(),
					ProfileActivity.class);
			// stopIntervalService();
			Log.d("LOG", "thong tin ca nhan 1");
			startActivity(i);
			Log.d("LOG", "thong tin ca nhan 2");
		} else if (menuItemId == 2) {
			final AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Đăng xuất khỏi hệ thống?")
					.setCancelable(true)
					.setIcon(R.drawable.alert_icon)
					.setTitle("Đăng xuất!")
					.setPositiveButton("Có",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									// Switching to Loggin form
									LogoutService logoutService = new LogoutService(getApplicationContext());
									logoutService.logout();
									Intent i = new Intent(
											getApplicationContext(),
											LoginActivity.class);
									// stopIntervalService();
									startActivity(i);
									// finish current activity.
									finish();
								}
							})
					.setNegativeButton("Không",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							});
			AlertDialog alert = builder.create();
			alert.getWindow().getAttributes().windowAnimations = R.style.DialogAnimationTopDown;
			alert.show();

		}
		return false;
	} // applyMenuOption

	private static View createTabView(final Context context, final String text,
			final int resId) {
		View view = LayoutInflater.from(context).inflate(R.layout.custom_tab,
				null);
		TextView tv = (TextView) view.findViewById(R.id.text);
		ImageView icon = (ImageView) view.findViewById(R.id.icon);
		icon.setImageResource(resId);
		tv.setText(text);
		return view;
	}
} // Menu1Act1
